﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Manson.WebServiceTest2
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            //dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
            //dispatcherTimer.Tick += DispatcherTimer_Tick;
            //dispatcherTimer.Start();
            this.txt_Time.Text = 100.ToString();
            this.txt_request1.Text = "IPBC27N088-D001";
            this.txt_request2.Text = "IPBC27N090-D001";
            this.txt_request3.Text = "IPBC26N060-D002";

        }

        private void DispatcherTimer_Tick(object? sender, EventArgs e)
        {
            //this.lbl_Time.Content = DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss");

        }

        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            JCETWebService.ServiceSoapClient serviceSoapClient = new JCETWebService.ServiceSoapClient(JCETWebService.ServiceSoapClient.EndpointConfiguration.ServiceSoap);

            var value = serviceSoapClient.DBTest("1");
        }

        public void GetWebParam()
        {
            //像调用普通命名空间下的Class一样，添加WebService对象
            JCETWebService.ServiceSoapClient serviceSoapClient = new JCETWebService.ServiceSoapClient(JCETWebService.ServiceSoapClient.EndpointConfiguration.ServiceSoap);

            //设置超时时间，默认超时时间为 1 min
            serviceSoapClient.InnerChannel.OperationTimeout = TimeSpan.FromMinutes(20);

            //serviceSoapClient.Open();
            int value = Convert.ToInt32(this.txt_Time.Text);
            if (txt_request1.Text.Length == 0 && txt_request2.Text.Length == 0 && this.txt_request3.Text.Length == 0)
            {
                MessageBox.Show("至少输入一个请求参数");
                return;
            }
            string request1 = txt_request1.Text;
            string request2 = txt_request2.Text;
            string request3 = txt_request3.Text;
            Task.Run(() =>
            {
                try
                {
                    if (request1.Length > 0)
                    {


                        for (int i = 0; i < value; i++)
                        {
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}:Request:IPBC27N088-D001");
                                OperateLog.AddLog($"Request:{request1}",request1 );
                            });
                            var result = serviceSoapClient.GetFT_LabelParam(request1 );
                            //serviceSoapClient.Close();
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}: Respond:{result}");
                                OperateLog.AddLog($"Respond: {result}",request1 );
                            });
                        }
                    }
                    if (request2 .Length > 0)
                    {
                        for (int i = 0; i < value; i++)
                        {
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}:Request:IPBC27N090-D001");
                                OperateLog.AddLog($"Request:{request2}", request2);
                            });
                            var result = serviceSoapClient.GetFT_LabelParam(request2);
                            //serviceSoapClient.Close();
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}: Respond:{result}");
                                OperateLog.AddLog($"Respond: {result}", request2);
                            });
                        }
                    }
                    if (request3.Length > 0)
                    {


                        for (int i = 0; i < value; i++)
                        {
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}:Request:IPBC26N060-D002");
                                OperateLog.AddLog($"Request:{request3}", request3);
                            });
                            var result = serviceSoapClient.GetFT_LabelParam(request3);
                           // serviceSoapClient.Close();
                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}: Respond:{result}");
                                OperateLog.AddLog($"Respond: {result}", request3);
                            });
                        }
                    }
                }
                catch (Exception ex)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        this.lv_Log.Items.Add($"{DateTime.Now.ToString("MM-dd HH:mm:ss")}: {ex.Message}");
                        OperateLog.AddLog(ex.Message);
                    });
                }
            });

        }

        private void btn_start_Click(object sender, RoutedEventArgs e)
        {
            GetWebParam();
        }
    }
}
